<?php
class SourceController extends  IndexController
{
    public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->Session        = Yaf_Registry::get("session");
        $this->SourceModel    = new SourceModel();
        $this->ArticleModel   = new ArticleModel();
        $this->UserLogsModel  = new LogsModel();
        $this->RobotsModel    = new RobotsModel();
        $this->TypicalModel   = new TypicalModel();
    }

    public function listAction()
    {
        $coll_site_info    = $this->SourceModel->getCollWebsiteinfo();
        $typicle_info      = $this->ArticleModel->articleType();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $source_info       = $this->SourceModel->selectSource($startLimit,$offset);
        $allCounts         = $this->SourceModel->getAllCount();
        $allpage=$this->utl::page('/Source/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $robots_info       = $this->RobotsModel->getRobotsInfo();
        $source_list       = array();
        
        foreach ($source_info AS $k => $v)
        {
            $source_list[$k] = $v;
            $source_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $source_list[$k]['rows'] = $allCounts[0]['rows'];
            $source_list[$k]['every_page'] = $offset;
            foreach ($typicle_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                    $source_list[$k]['typical_name'] = $vs['name'];break;
                }
            }
            foreach ($robots_info  AS $ks => $vs)
            {
                if ($v['robot_id'] === $vs['robot_id'])
                {
                    $source_list[$k]['robot_name'] = $vs['robot'];break;
                }
            }
            foreach ($coll_site_info  AS $ks => $vs)
            {
                if ($v['site_id'] === $vs['website_id'])
                {
                    $source_list[$k]['website_name'] = $vs['remark'];break;
                }
            }
        }
        $this->getView()->assign("source_list",$source_list);
        $this->getView()->assign("article_type_list",$typicle_info);
        $this->getView()->assign("robots_list",$robots_info);
        $this->getView()->assign("coll_site_list",$coll_site_info);

        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $coll_site_info    = $this->SourceModel->getCollWebsiteinfo();
        $typicle_info      = $this->ArticleModel->articleType();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $source_info       = $this->SourceModel->selectSource($startLimit,$offset);
        $allCounts         = $this->SourceModel->getAllCount();
        $allpage=$this->utl::page('/Source/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $robots_info       = $this->RobotsModel->getRobotsInfo();
        $source_list       = array();
        
        foreach ($source_info AS $k => $v)
        {
            $source_list[$k] = $v;
            $source_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $source_list[$k]['rows'] = $allCounts[0]['rows'];
            $source_list[$k]['every_page'] = $offset;
            foreach ($typicle_info  AS $ks => $vs)
            {
                if ($v['article_type_id'] === $vs['article_type_id'])
                {
                    $source_list[$k]['typical_name'] = $vs['name'];break;
                }
            }
            foreach ($robots_info  AS $ks => $vs)
            {
                if ($v['robot_id'] === $vs['robot_id'])
                {
                    $source_list[$k]['robot_name'] = $vs['robot'];break;
                }
            }
            foreach ($coll_site_info  AS $ks => $vs)
            {
                if ($v['site_id'] === $vs['website_id'])
                {
                    $source_list[$k]['website_name'] = $vs['remark'];break;
                }
            }
        }
        $this->getView()->assign("source_list",$source_list);
        exit(json_encode($source_list, true));
    }

    public function currentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $source_info       = $this->SourceModel->selectSource($startLimit,$offset);
        $allCounts         = $this->SourceModel->getAllCount();
        $allpage=$this->utl::page('/Source/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Source', 'list');
            return;
        }
        $rs = $this->SourceModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "source";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }



    public function addAction()
    {
        if ($this->getRequest()->isPost())
        {
            $post           = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->SourceModel->add($post);        
            $session_add    = array();
            $collection_add = array();
            //$post = $this->getRequest()->getPost();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "source";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);

            $source_id = $this->SourceModel->getMaxId();
            $collection_add['collection_robot_id'] = $post['robot_id'];
            $collection_add['collection_source_id'] = $source_id[0]['id'];
            $collection_add['url'] = $post['url'];
            $collection_add['article_id'] = $post['article_type_id'];;
            $this->UserLogsModel->addColl($collection_add);
            if (false === $rs)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');

        }
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Source', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->SourceModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "source";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function sourceAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            header('Content-type:application/json;charset:utf8');
            $rs = $this->ArticleModel->articleType();
            if (false === $rs)
            {
                exit('{"status":false,"info":"采集源查找失败！"}');
                return;
            }
            $result  =  json_encode($rs);
            exit($result);
        }
    }

    public function robotAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            $rs   = $this->SourceModel->getCollWebsiteinfo();
            $coll_robot = $this->SourceModel->getCollWebsiteByRobot($post['article_type_id']);
            foreach ($coll_robot as $key => $value) {
                $coll_robot[$key] = $value;
                foreach ($rs as $ks => $vs) {
                   if($value['website_id'] == $vs['website_id']){
                        $coll_robot[$key]['website_name'] = $vs['remark'];
                        $coll_robot[$key]['article_type_id'] = $post['article_type_id'];
                   }
                }
            }
            header('Content-type:application/json;charset:utf8');
            if (false === $coll_robot)
            {
                exit('{"status":false,"info":"采集机器人查找失败！"}');
                return;
            }
            $result  =  json_encode($coll_robot);
            exit($result);
        }
    }

    public function typicalAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $coll_robot = $this->SourceModel->getCollRobotByWebsite($post);
            if (false === $coll_robot)
            {
                exit('{"status":false,"info":"采集机器人类型查找失败！"}');
                return;
            }
            $result  =  json_encode($coll_robot);
            exit($result);
        }
    }

    public function urlAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            header('Content-type:application/json;charset:utf8');
            $post = $this->getRequest()->getPost();
            $rs = $this->SourceModel->getCollTypicalByRobot($post['robot_id']);
            if (false === $rs)
            {
                exit('{"status":false,"info":"采集源查找失败！"}');
                return;
            }
            $result  =  json_encode($rs);
            exit($result);
        }
    }
}
?>